﻿using System;
using System.Linq;
using System.Web.UI;
using RBOSS.Business.Presenters;
using RBOSS.Business.Views;
using RBOSS.WebUI.Models;

namespace RBOSS.WebUI
{
    public partial class ShoppingCartForm : MasterSite, IShoppingCartView
    {
        private readonly ShoppingCartPresenter _shoppingCartPresenter;
        // public event EventHandler RemoveProductClick;
        public ShoppingCartForm()
        {
            _shoppingCartPresenter = new ShoppingCartPresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            UpdateProductList();

            WireupEven();
        }

        private void UpdateProductList()
        {
            var products = _shoppingCartPresenter.GetProducts();
            gvProduct.DataSource = products;
            gvProduct.DataBind();

            ShowCheckoutButton(gvProduct.Rows.Count > 0);
            
        }

        private void WireupEven()
        {
            gvProduct.SelectedIndexChanged += RemoveProductClicked;
            
        }

        

        private void RemoveProductClicked(object sender, EventArgs e)
        {
            _shoppingCartPresenter.RemoveProduct((int)gvProduct.DataKeys[gvProduct.SelectedIndex].Value);
            UpdateProductList();
        }

        protected override void InvokeCheckoutClick(object sender, EventArgs e)
        {

            Response.Redirect("BillingInformationForm.aspx");
        }
    }
}